package com.example.mynewsapp.adapter

import android.content.Context
import android.provider.Settings.Global.getString
import android.util.Log
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.example.mynewsapp.R
import com.example.mynewsapp.fragment.Fragment.TAB_TITLES
import com.example.mynewsapp.fragment.Fragment.TAB_TITLES_CN
import com.example.mynewsapp.fragment.news.*
import com.example.mynewsapp.util.Utils.getCountry

class NewsSectionAdapter(private val context: Context, fm: FragmentManager)
    : FragmentPagerAdapter(fm) {

    //顶部tablayout的字符
//    private lateinit var TAB_TITLES :Array<String>

    override fun getItem(position: Int): Fragment {
        // 顶部有多少个tab，就会运行多少次（初次会加载第一第二，第三第四要点第三个tab才会运行）
        /*******新建Fragment******/
//        Log.d("WY+","运行第几次："+(position+1))
        when (position) {
            0 -> {
                return FragmentHeadline.newInstance(0)
            }
            1 -> {
                return FragmentSports.newInstance(0)
            }
            2 -> {
                return FragmentTechnology.newInstance(0)
            }
            3 -> {
                return FragmentBusiness.newInstance(0)
            }
            4 -> {
                return FragmentHealth.newInstance(0)
            }
            5 -> {
                return FragmentEntertainment.newInstance(0)
            }
        }
        return FragmentHeadline.newInstance(0)

    }

    override fun getPageTitle(position: Int): CharSequence? {
        if (getCountry() == "cn") {
            return TAB_TITLES_CN[position]
        }
        return TAB_TITLES[position]
    }

    override fun getCount(): Int {
        // 顶部显示多少个页，不要超过TAB_TITLES的总数.
        return TAB_TITLES.size
    }
}